import paramiko

class RemotePc():
    def __init__(self, host, user, passw):
        self.host = host
        self.user = user
        self.passw = passw
        self.sshClient = None
        self.sftpClient = None

    def _ssh_connect(self, user=None, passw=None):
        if not user:
            user = self.user
        if not passw:
            passw = self. passw
        if not self.sshClient:
            self.sshClient = paramiko.SSHClient()
            self.sshClient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            self.sshClient.connect(hostname=self.host, port=22, username=self.user, password=self.passw)
        

    def _sftp_connect(self, user=None, passw=None):
        if not self.sshClient:
            self._ssh_connect()
        self.sftpClient = paramiko.SFTPClient.from_transport(self.sshClient.get_transport())

    def send_command(self, cmd):
        self._ssh_connect()
        stdin, stdout, stderr = self.sshClient.exec_command(cmd)
        res, err = stdout.read(), stderr.read()
        result = res if res else err
        self.sshClient.close()
        return result.decode()

    def getfile(self, remote_file, local_file):
        self._sftp_connect()
        self.sftpClient = self.sshClient.open_sftp()
        self.sftpClient.get(remote_file, local_file)
        self.sftpClient.close()

    def putfile(self, local_file, remote_file):
        self._sftp_connect()
        self.sftpClient = self.sshClient.open_sftp()
        self.sftpClient.put(local_file, remote_file)
        self.sftpClient.close()

    def exsitfile(self, remote_file):
        self._sftp_connect()
        self.sftpClient = self.sshClient.open_sftp()
        self.sftpClient.put(local_file, remote_file)
        self.sftpClient.close()

    # def rmfile(self, remote_file):
